package com.mew.chess.core.game.pokdeng;

import com.mew.chess.core.common.handler.ChessHandlerMapCmdType;
import com.mew.chess.core.common.handler.ChessPlayAnnotation;
import com.mew.chess.core.common.handler.ChessPlayHandler;
import com.mew.chess.core.common.handler.ChessPushMessageUtil;
import com.mew.chess.core.game.pokdeng.game.PokDengGameManager;
import com.mew.chess.core.game.pokdeng.game.PokDengSeatManager;
import com.mew.chess.core.game.pokdeng.game.PokDengTableManager;
import com.mew.chess.core.game.pokdeng.game.common.PokDengLinkType;
import com.mew.chess.core.game.pokdeng.game.common.PokDengPushCMDMapType;
import com.mew.chess.core.game.pokdeng.game.core.CardUtil;
import com.mew.chess.core.game.pokdeng.game.message.*;
import com.mew.common.exception.ResponseCode;
import com.mew.common.rest.bo.GameSession;
import com.zebra.remoting.protocol.ZebraCommand;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;


/**
 * @author dylin
 */
@ChessPlayAnnotation(ChessHandlerMapCmdType.PokDengAsk)
@Service
@Slf4j
public class PokDengAskHandler implements ChessPlayHandler<AskRequest> {
    @Override
    public void handle(ZebraCommand<AskRequest> command, GameSession session) {
        AskRequest askMessage = command.getRequest();
        
        long userId = session.getUserId();
        int roomId = session.getRoomId();
        int tableId = session.getTableId();
        int seatNo = session.getSeatNo();
        
        if (askMessage.getSeatNo()<0||askMessage.getTableId()<0) {
            command.sendFailureCode(ResponseCode.ParamInvalid.code());
            return;
        }
        PokDengTableManager tableManager = PokDengGameManager.getInstance().getController(roomId).getTableManager(tableId);
        PokDengSeatManager seat = tableManager.getSeatMap().get(seatNo);
        if (null == tableManager ||null == seat|| null == seat.getUser()) {
            log.error("PokDengRoomManager is null userId:{} roomId: {}", userId, roomId);
            command.sendFailureCode(ResponseCode.LoginUn.code());
            return;
        }
        if (tableManager.getLinkType() != PokDengLinkType.AskCard.code()||tableManager.getAskNowSeat()!=seatNo) {
            log.error("PokDengRoomManager stop bet userId:{} roomId: {}", userId, roomId);
            command.sendFailureCode(ResponseCode.StopBet.code());
            return;
        }
        UserMessage user=seat.getUser();
        if(user.getUserId()!=userId){
            log.error("PokDengRoomManager user error userId:{} roomId: {}", userId, roomId);
            command.sendFailureCode(ResponseCode.BetError.code());
            return;
        }
        if(user.getCards().size()!=2){
            log.error("PokDengRoomManager repetitive bet userId:{} roomId: {}", userId, roomId);
            command.sendFailureCode(ResponseCode.BetError.code());
            return;
        }
        int card=CardUtil.deal(tableManager.getCardLib(),seatNo,tableManager.isControl(),user.getCards().size());
        user.getCards().add(card);
        CardUtil.computeCard(user);

        PushOtherAskCardMessage message = new PushOtherAskCardMessage();
        message.setAskCard(card);
        message.setCard(CardMessage.buildByUser(seat));
        List<GameSession> sessionList = tableManager.getOnlineUser().values().stream().filter(u -> u.getUserId() != userId).collect(Collectors.toList());
        ChessPushMessageUtil.broadcastMessage(sessionList, PokDengPushCMDMapType.OtherUserAsdCardMessage.code(),message);

        command.sendMessage(AskResponse.builder().askCard(card).card(CardMessage.buildByUser(seat)).build());
        //调用这个方法 会跳过当前请求
        tableManager.getScheduleManager().doAskCard();
    }
}
